﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MMM.Core.Domain;
using ServiceStack.Text;

namespace MMM.Services.Operations
{
    [Serializable]
    public class PageGet<T, Tid> : EntityBase<T, Tid>, IPageGet where T : EntityBase<T, Tid>
    {
        public PageGet()
        {
            Start = 0;
            Limit = 20;
            Page = 1;
            Flag = 0;
            IsUpFile = false;
            outPutName = "report";
        }
        public string outPutName { get; set; }
        public int Start { get; set; }
        public int Page { get; set; }
        public int Limit { get; set; }
        public string Sort { get; set; }
        public int Flag { get; set; }
        public string Filter { get; set; }
        public bool IsUpFile { get; set; }

        public string orderName { get; set; }
        public string head { get; set; }

        public IList<Order> GetSort()
        {
            if (string.IsNullOrWhiteSpace(Sort))
            {
                return new List<Order>();
            }
            var discussions = JsonSerializer.DeserializeFromString<List<JsonObject>>(Sort)
                      .ConvertAll(x => new Order
                      {
                          property = x.Get("property"),
                          direction = x.Get("direction")
                      });
            return discussions;
        }

        public IList<Filter> GetFilter()
        {
            if (Filter == null)
            {
                return null;
            }
            var discussions = JsonSerializer.DeserializeFromString<List<JsonObject>>(Filter)
                     .ConvertAll(x => new Filter
                     {
                         Property = x.Get("property"),
                         Value = x.Get("value")
                     });
            return discussions;
        }
    }

    [Serializable]
    public class PageGet<T> : PageGet<T, Guid?> where T : EntityBase<T, Guid?>
    {
    }

    [Serializable]
    public class Order
    {
        public string property { get; set; }
        public string direction { get; set; }
    }



    [Serializable]
    public class Filter
    {
        public string Property { get; set; }
        public string Value { get; set; }
        /// <summary>
        /// 转换成sql (全部相等)
        /// </summary>
        /// <returns></returns>
        public string ToSqlEque()
        {
            return string.Format("{0}='{1}'", Property, Value);
        }

        /// <summary>
        /// 转换成sql (模糊查询)
        /// </summary>
        /// <returns></returns>
        public string ToSqlLike()
        {
            return string.Format("{0} like '%{1}%'", Property, Value);
        }

        /// <summary>
        /// 转换成sql (大于不等于)
        /// </summary>
        /// <returns></returns>
        public string ToSqlGreaterThan()
        {
            return string.Format("{0} > '{1}'", Property, Value);
        }

        /// <summary>
        /// 转换成sql (大于等于)
        /// </summary>
        /// <returns></returns>
        public string ToSqlGreaterThanAndEque()
        {
            return string.Format("{0} >= '{1}'", Property, Value);
        }

        /// <summary>
        /// 转换成sql (小于等于)
        /// </summary>
        /// <returns></returns>
        public string ToSqlLessThan()
        {
            return string.Format("{0} <= '{1}'", Property, Value);
        }

        /// <summary>
        /// 转换成sql (小于不等于)
        /// </summary>
        /// <returns></returns>
        public string ToSqlLessThanAndAEque()
        {
            return string.Format("{0} < '{1}'", Property, Value);
        }


    }

}
